package com.rural.bus;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rural.bus.model.MyPoiInfo;
import com.rural.bus.model.Order;
import com.rural.bus.model.Railway;
import com.rural.bus.utils.DateUtil;
import com.rural.bus.view.SlideItemDialog;
import com.rural.bus.view.SlideItemDialog.OnDialogDismissListener;

public abstract class BaseRailwayActivity extends BaseActivity {
	public static final String TAG = "com.rural.bus.BaseRailwayActivity";
	public static final int REQUEST_MAP_START_ADDRESS = 0X01;
	public static final int RESULT_MAP_START_ADDRESS = 0X02;
	public static final int REQUEST_MAP_END_ADDRESS = 0X03;
	public static final int RESULT_MAP_END_ADDRESS = 0X04;
	// private static final int SET_NAVIGATION_DISTANCE = 0x90;

	protected int mOrderType = 0;

	protected MyPoiInfo mStartAddress, mEndAddress;

	protected TextView mTvTime;

	protected TextView mTvNumber;
	protected TextView mTvPrice;
	protected TextView mTvCarType;
	protected Button mBtSubmit;
	private CheckBox checkbox;
	private LinearLayout ll_person_info, ll_select_car_type, ll_select_contacts;
	
	/** 乘车人数 */
	protected int number = 1;
	/** 乘车时间 */
	protected long taketime;
	/** 是否是本人 */
	protected boolean isSelf = true;
	protected int carTypeIndex = 0; // 所选择车型 索引
	protected int serviceTypeIndex; // 所选服务类型 索引

	protected EditText etPassengerName, etPassengerPhone;
	protected String passengerName, passengerPhone;
	/***
	 * 估算出来的距离
	 */
	protected double dis = 0;
	protected Railway mCurrentRailway;
	protected EditText mEtRemarks;

	private SlideItemDialog selectNumber, selectType;
	
	protected LinearLayout mLlTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_distance);

		initView();
		setListeners();
		initData();
	}

	@Override
	protected void initView() {

		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mTvTime = (TextView) findViewById(R.id.tv_time);

		mTvNumber = (TextView) findViewById(R.id.tv_number);
		mTvPrice = (TextView) findViewById(R.id.tv_price);
		mBtSubmit = (Button) findViewById(R.id.bt_submit);
		mTvCarType = (TextView) findViewById(R.id.tv_car_type);
		mEtRemarks = (EditText) findViewById(R.id.et_remarks);
		mLlTime=(LinearLayout) findViewById(R.id.ll_time);
		
//		tv_rent_balf_day = (TextView) findViewById(R.id.tv_rent_balf_day);
//		tv_rent_day = (TextView) findViewById(R.id.tv_rent_day);

		mTvCarType.setText("舒服车型");
		carTypeIndex = 1;

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);

		checkbox = (CheckBox) findViewById(R.id.checkbox);
		ll_person_info = (LinearLayout) findViewById(R.id.ll_person_info);
		ll_select_car_type = (LinearLayout) findViewById(R.id.ll_select_car_type);
		ll_select_contacts = (LinearLayout) findViewById(R.id.ll_select_contacts);

		etPassengerName = (EditText) findViewById(R.id.et_passenger_name);
		etPassengerPhone = (EditText) findViewById(R.id.et_passenger_phone);

	}

	@Override
	protected void initData() {
		if (mCurrentUser == null) {
			startActivity(LoginActivity.class);
			finish();
			return;
		}

		serviceTypeIndex = getIntent().getExtras().getInt("tag", 0);
		ll_select_car_type.setVisibility(View.VISIBLE);
		switch (serviceTypeIndex) {
		case 1:
			mBtMiddle.setText("预约用车");
			mOrderType = Order.ORDERTYPE_SUBSCRIBE;
			break;
		case 2:
			mBtMiddle.setText("专线");
			mOrderType = Order.ORDERTYPE_SPEC;
			ll_select_car_type.setVisibility(View.GONE);
			break;
		case 3:
			mBtMiddle.setText("短租");
			mOrderType = Order.ORDERTYPE_RENT;
			break;
		default:
			mBtMiddle.setText("马上用车");
			mOrderType = Order.ORDERTYPE_ATONCE;
		}

		mTvTime.setText(DateUtil.getCurrentTime("yyyy年MM月dd日 HH:mm"));
	}


	@Override
	protected void setListeners() {

		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// reset passenger info,because last info will cause error
				passengerName = null;
				passengerPhone = null;

				if (isChecked) {
					isSelf = true;
					ll_person_info.setVisibility(View.GONE);
					ll_select_contacts.setVisibility(View.GONE);
				} else {
					isSelf = false;
					ll_person_info.setVisibility(View.VISIBLE);
					ll_select_contacts.setVisibility(View.VISIBLE);
				}
			}
		});

		mTvNumber.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				final String[] arr = new String[] { "1", "2", "3", "4" };
				// new AlertDialog.Builder(BaseRailwayActivity.this)
				// .setTitle("人数")
				// .setSingleChoiceItems(arr,
				// number-1, new DialogInterface.OnClickListener() {
				//
				// @Override
				// public void onClick(DialogInterface arg0, int arg1) {
				// arg0.dismiss();
				// mTvNumber.setText(arr[arg1]);
				// number=new Integer(arr[arg1]);
				// }
				// }).setNegativeButton("取消", new
				// DialogInterface.OnClickListener() {
				//
				// @Override
				// public void onClick(DialogInterface arg0, int arg1) {
				// arg0.dismiss();
				// }
				// }).show();
				if (selectNumber == null) {
					selectNumber = new SlideItemDialog(BaseRailwayActivity.this, "选择人数",arr, 0, new OnDialogDismissListener() {

						@Override
						public void onConfirmClicked(int index) {
							// TODO Auto-generated method stub
							mTvNumber.setText(arr[index]);
							number = new Integer(arr[index]);
						}

						@Override
						public void onCancelClicked() {
							// TODO Auto-generated method stub

						}

					});
				}
				selectNumber.show();
			}
		});

		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		mTvCarType.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ("经济类型", 0)、("舒服型", 1)、("商务型", 2)、("豪华型", 3)

				final String[] arrTypes = new String[] { "经济车型", "舒服车型", "商务车型", "豪华车型" };
				if (selectType == null) {
					selectType = new SlideItemDialog(BaseRailwayActivity.this, "选择车型",arrTypes, 1, new OnDialogDismissListener() {

						@Override
						public void onConfirmClicked(int index) {
							// TODO Auto-generated method stub
							carTypeIndex = index;
							switch (index) {
							case 0:
								mTvCarType.setText("经济车型");
								break;
							case 1:
								mTvCarType.setText("舒服车型");
								break;
							case 2:
								mTvCarType.setText("商务车型");
								break;
							case 3:
								mTvCarType.setText("豪华车型");
								break;
							}
						}

						@Override
						public void onCancelClicked() {
							// TODO Auto-generated method stub

						}

					});
				}
				selectType.show();
			}
		});

		ll_select_contacts.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
				BaseRailwayActivity.this.startActivityForResult(intent, 1);
			}

		});
		
//		tv_rent_balf_day.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				tv_rent_day.setBackgroundResource(R.drawable.bg_white_recetange);
//				tv_rent_balf_day.setBackgroundResource(R.drawable.bg_orange_recetange_left);
//				
//			}
//		});
		
//		tv_rent_day.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				tv_rent_balf_day.setBackgroundResource(R.drawable.bg_white_recetange);
//				tv_rent_day.setBackgroundResource(R.drawable.bg_orange_recetange_left);
//				
//			}
//		});

	}

//	/***
//	 * 选择时间对话框，子类必须复写它，并且设置mTvTime时间
//	 */
//	protected abstract void timeDialog();

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			//通过通讯录设置个人数据
			if (resultCode == Activity.RESULT_OK) {
				ContentResolver reContentResolverol = getContentResolver();
				Uri contactData = data.getData();
				@SuppressWarnings("deprecation")
				Cursor cursor = managedQuery(contactData, null, null, null, null);
				cursor.moveToFirst();
				String username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
				Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
				while (phone.moveToNext()) {
					String usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//					Toast.makeText(this, usernumber + " (" + username + ")", Toast.LENGTH_LONG).show();
					etPassengerName.setText(username);
//					usernumber =	usernumber.replaceAll(" ", "");
					etPassengerPhone.setText(usernumber);
				}

			}
			break;

		default:
			break;
		}

	}

}
